//
//  ManageViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/26.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "ManageViewController.h"
#import "ManageTableViewCell.h"
#import "WriteViewController.h"
#import "EditAddressViewController.h"

@interface ManageViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@end

@implementation ManageViewController
{
    UITextView* family;

    UITableView * _managerTable;
    NSMutableArray * arr;
    NSMutableArray * _shuJuAry;
    UIView * _nullview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawNavWithTitle:@"管理收货地址" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    self.view.backgroundColor=[UIColor colorWithHue:0.00 saturation:0.00 brightness:0.96 alpha:1.00];
    [self tableview];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tableReloade) name:@"diZhitableShuaXin" object:nil];
    [self requestData];
}

-(void)requestData{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [TCUtils showLoadingWithView:self.view];
    [NetWorking NEWjieKouQingQiu:DZ_chaXunDiZhi ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error){
        [TCUtils hiddonLoadingWithView:self.view];
        if (!error) {
    
            if ([[dic objectForKey:Code]integerValue] == 200) {
                
                [_nullview removeFromSuperview];
                NSArray *data = [dic objectForKey:Response];
                _shuJuAry = [[NSMutableArray alloc]init];
                
                [data enumerateObjectsUsingBlock:^(id   obj, NSUInteger idx, BOOL *  stop) {
                    addressModel *address = [addressModel objectFromDictionary:obj];
                    [_shuJuAry addObject:address];
                }];
                
                if (_shuJuAry.count == 0) {
                    [self nill];
                }
                 [_managerTable reloadData];
                
            }
        }
    }];
}

-(void)tableReloade{
    [self requestData];
}

-(void)nill{
    _nullview = [[UIView alloc]initWithFrame:CGRectMake(0, 74, KScreenWidth, 50)];
    _nullview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_nullview];
    UILabel * new = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    new.text=@"新增地址";
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth-30, 15, 20, 20)];
    image.image = [UIImage imageNamed:@"you"];
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 50)];
    [button addTarget:self action:@selector(addPriae) forControlEvents:UIControlEventTouchUpInside];
    [_nullview addSubview:image];
    [_nullview addSubview:new];
    [_nullview addSubview:button];
    
}
//进入添加地址页面
-(void)addPriae{
    
    WriteViewController * jifen = [[WriteViewController alloc]init];
    [self.navigationController pushViewController:jifen animated:YES];
}
-(void)tableview{
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, KScreenHeight-55, KScreenWidth, 55)];
    button.backgroundColor = [UIColor colorWithHue:0.05 saturation:1.00 brightness:1.00 alpha:1.00];
    [button setTitle:@"添加新地址" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tianJia) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    _managerTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 66, KScreenWidth, KScreenHeight-66-55)];
    _managerTable.separatorStyle = UITableViewCellSelectionStyleNone;
    _managerTable.dataSource = self;
    _managerTable.delegate = self;
    [_managerTable registerClass:[ManageTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_managerTable];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _shuJuAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 132;
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ManageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    addressModel *address = [_shuJuAry objectAtIndex:indexPath.row];
    if ( address.defaultAddress == 1) {
        cell.moRen.textColor = [UIColor redColor];
        cell.imageButton.image=[UIImage imageNamed:@"√"] ;
    }else{
        cell.moRen.textColor = [UIColor blackColor];
        cell.imageButton.image=[UIImage imageNamed:@"Ȧ"] ;
    }
    cell.name.text = address.consignee;
    cell.call.text = address.mobile;
    cell.prize.text = [NSString stringWithFormat:@"%@%@%@%@",address.province,address.city,address.district,address.address];
    cell.remove.tag = indexPath.row;
    cell.bianJi.tag = indexPath.row;
    cell.imageButton.tag = indexPath.row;
    [cell.remove addTarget:self action:@selector(remove:) forControlEvents:UIControlEventTouchUpInside];
    [cell.bianJi addTarget:self action:@selector(bianJi:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    addressModel *address = [_shuJuAry objectAtIndex:indexPath.row];
    [NetWorking NEWjieKouQingQiu:DZ_sheWeiMoRen ANDIP:IP And:@{@"addressId":address.addressId,@"userName":[self defaultLoadUserName]} anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                [self requestData];
                if (_timp == 1) {//判断是否为支付界面push过来
                    [self.navigationController popViewControllerAnimated:YES];
                }
              }
        }
    }];
}
//删除地址
-(void)remove:(UIButton *)button{
    
    [TCUtils showAlertViewWithTitle:@"提示" message:@"确定删除此收货地址?" delegate:self cancel:@"取消" other:@"确定" tag:(int)button.tag];
   }
//编辑地址
-(void)bianJi:(UIButton *)button{
    [[NSUserDefaults standardUserDefaults] setInteger:button.tag forKey:@"num"];
    EditAddressViewController * editDiZhi = [[EditAddressViewController alloc]init];
    editDiZhi.dizhiDic = [_shuJuAry objectAtIndex:button.tag];
    [self.navigationController pushViewController:editDiZhi animated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        addressModel *add = [_shuJuAry objectAtIndex:alertView.tag];
        NSMutableDictionary *deleteDic = [[NSMutableDictionary alloc]init];
        [deleteDic setObject:[self defaultLoadUserName] forKey:@"userName"];
        [deleteDic setObject:add.addressId forKey:@"addressId"];
        
        [NetWorking NEWjieKouQingQiu:DZ_shanChuDiZhi ANDIP:IP And:deleteDic anBlock:^(NSDictionary *dic, NSError *error) {
            
            if (!error) {
                if ([[dic objectForKey:Code]integerValue] == 200) {
                    //删除成功
                    [_shuJuAry removeObjectAtIndex:alertView.tag];
                    [_managerTable reloadData];
                }
            }
        }];

    }
}

//选择地址
-(void)xuanZe:(UIButton *)button{
   
    
}
//添加新地址
-(void)tianJia{
    WriteViewController * jifen = [[WriteViewController alloc]init];
    [self.navigationController pushViewController:jifen animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
